<?php
// 抓取漫画列表页
set_time_limit(0);

include_once('lib/config.php');
include_once('lib/Db.class.php');
include_once('lib/Model.class.php');
include_once('lib/Upyun.class.php');
include_once('lib/Image.class.php');
include_once('lib/Http.class.php');
include_once('lib/Cut.class.php');
include_once('lib/function.php');

$redis = new Redis();
$redis->connect(REDIS_IP,"6379");  //php客户端设置的ip及端口
if(!$redis) die('redis connect fail');

$queue_zlist_fetch_key = 'Queue_zlist_fetch';
$all_list_fetched = array('www.leshen.com');

$model = new Model;
$http  = new Http;
$cut   = new Cut;

$pid = getmypid();
$redis->hSet(MH_PIDS, $pid, date('Y-m-d H:i:s').'||mh_zlist');

$limit = 20;
while($limit--) {
  $stop = $redis->get('LNPAN_MH_QUEUE_STOP');
  if($stop) {
    break;
  }
  $new_count = $old_count = $failed_count = 0;

  $queue = $redis->lPop($queue_zlist_fetch_key);
  //$redis->lPush($queue_zlist_fetch_key, $queue);
  if($queue) {
    list($type, $url) = explode('||', $queue);
    $info = array('type'=>$type, 'link'=>$url);
  
    $parse_url = parse_url($url);

    $opt_data = array(
      CURLOPT_REFERER => $url,
      CURLOPT_HTTPHEADER => array(
        'Referer' => $url,
        'Host' => $parse_url['host'],
        'Pragma' => 'no-cache',
      ),
    );
    $http->opt = $opt_data;
    $responses = $http->curl_http( array($url) );
    $file = $responses[$url]['results'];
    $result = $cut->getList($file, $info);

    $info['http_code'] = $responses[$url]['info']['http_code'];
    if($result['error']) {
      $info['error'] = '抓取内容错误';
      //$info['responses'] = $responses;
    }
    elseif(count($result)) {
      foreach ($result as $item) {
        $push_id = false;
        $old = $model->find("news_zcopy", "link='$item[link]'");
        if($old) {
          $old_count++;
          $push_id = $old['id'];
        }
        elseif($model->add("news_zcopy", $item)) {
          $news_id = $model->insert_id();
          $new_count++;
          $info['new_ids'][] = $news_id;
          $push_id = $news_id;
        }
        else {
          $failed_count++;
        }
        //新漫画加入热更队列
        if($push_id && in_array($parse_url['host'], $all_list_fetched)) {
          $lsize = $redis->lSize('Queue_zcopy_hot_fetch');
          if($lsize < 100) {
            $redis->rPush('Queue_zcopy_hot_fetch', $push_id);
          }   
        }
      }
    }
    else {
      $info['error'] = '解析错误';
    }

    $info['new_count'] = $new_count;
    $info['old_count'] = $old_count;
    $info['failed_count'] = $failed_count;
  }

  if($new_count > 0) {
    $info['new_date'] = date('Y-m-d H:i:s');
    $redis->lPush('New_zlist', print_r($info, true));
  }
}

$model->close();
$redis->hDel(MH_PIDS, $pid);